package pluginMain;

import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

import net.milkbowl.vault.economy.*;

public class MoneyRobber extends JavaPlugin implements Listener {
	public final String PLUGIN_NAME = "Money Robber Plugin";
	public final Logger CONSOLE_LOGGER = this.getLogger();
	// config
	public String AttakerTips, DeaderTips, BroadcastTips;
	public String AttakerNotEnought_Attaker, AttakerNotEnought_Deader,
			DeadNotEnought_Attaker, DeadNotEnought_Deader;
	public boolean EnableBroadcast;
	public int MaxRob, MinRob, MinMoneyToRob;
	public Economy eco;
	// //////////////////////////////////////
	public void LOGCONSOLE(String msg) {
		CONSOLE_LOGGER.info(msg);
	}

	@Override
	public void onEnable() {
		AttakerTips = getConfig().getString("AttakerTips");
		DeaderTips = getConfig().getString("DeaderTips");
		BroadcastTips = getConfig().getString("BroadcastTips");
		AttakerNotEnought_Attaker = getConfig().getString(
				"AttakerNotEnought_Attaker");
		AttakerNotEnought_Deader = getConfig().getString(
				"AttakerNotEnought_Deader");
		DeadNotEnought_Attaker = getConfig()
				.getString("DeadNotEnought_Attaker");
		DeadNotEnought_Deader = getConfig().getString("DeadNotEnought_Deader");

		EnableBroadcast = getConfig().getBoolean("EnableBroadcast");

		MaxRob = getConfig().getInt("MaxRob");
		MinRob = getConfig().getInt("MinRob");
		MinMoneyToRob = getConfig().getInt("MinMoneyToRob");
		this.getServer().getPluginManager().registerEvents(this, this);
		RegisteredServiceProvider<Economy> es = getServer()
				.getServicesManager().getRegistration(Economy.class);
		if (es == null) {
			CONSOLE_LOGGER.info("ERROR ::  You must install valut.");
			setEnabled(false);
		} else {
			eco = es.getProvider();
		}
		LOGCONSOLE(PLUGIN_NAME + " has been started.");
	}

	@Override
	public void onDisable() {
		LOGCONSOLE(PLUGIN_NAME + " has been disabled.");
	}

	@EventHandler
	public void onEntityInjured(EntityDamageByEntityEvent event) {
		if (event.getEntity() instanceof Player) {
			if (event.getDamager() instanceof Player) {
				Player source = (Player) event.getDamager();
				Player target = (Player) event.getEntity();
				double damage = event.getFinalDamage();
				if (damage >= target.getHealth()) {
					double rnd = Math.random();
					while (rnd < MinRob)
						rnd = Math.random() * MaxRob + MaxRob * 0.1;
					// kill cause:
					if (getPlayerMoney(source) < MinMoneyToRob) {
						source.sendMessage(sendMessageFliter(target, source,
								(int) rnd, AttakerNotEnought_Attaker));
						target.sendMessage(sendMessageFliter(target, source,
								(int) rnd, AttakerNotEnought_Deader));
						return;
					} else if (getPlayerMoney(target) < MinMoneyToRob) {
						source.sendMessage(sendMessageFliter(target, source,
								(int) rnd, DeadNotEnought_Attaker));
						target.sendMessage(sendMessageFliter(target, source,
								(int) rnd, DeadNotEnought_Deader));
					} else {
						addMoneyToPlayer(source, MinRob);
						addMoneyToPlayer(target, -MinRob);
						source.sendMessage(sendMessageFliter(target, source,
								(int) rnd, AttakerTips));
						target.sendMessage(sendMessageFliter(target, source,
								(int) rnd, DeaderTips));
					}
				}
			}
		}
	}

	public String sendMessageFliter(Player target, Player source, int rob,
			String message) {
		return ChatColor.translateAlternateColorCodes(
				'&',
				message.replaceAll("%a", String.valueOf(rob))
						.replaceAll("%c", getMoneyCurrentry())
						.replaceAll("%t", target.getName())
						.replaceAll("%s", source.getName())
						.replaceAll("%m", String.valueOf(MinRob)));

	}

	public String getMoneyCurrentry() {
		return eco.currencyNameSingular();
	}

	@SuppressWarnings("deprecation")
	public void addMoneyToPlayer(String playerName, int amount) {
		if (amount > 0) {
			eco.depositPlayer(playerName, amount);
		} else {
			eco.withdrawPlayer(playerName, amount);
		}
	}

	public void addMoneyToPlayer(Player playerName, int amount) {
		addMoneyToPlayer(playerName.getName(), amount);
	}

	@SuppressWarnings("deprecation")
	public int getPlayerMoney(String PlayerName) {
		return (int) eco.getBalance(PlayerName);
	}

	public int getPlayerMoney(Player PlayerName) {
		return getPlayerMoney(PlayerName.getName());
	}

}
